﻿using StoreSQLSugar_.DBUtility;
using StoreSQLSugar_.Helpers;
using StoreSQLSugar.Models;
using System;
using System.Windows.Forms;

namespace StoreSQLSugar.Forms.UserTypeFrm
{
    public partial class UserTypeAddEditFrm : Form
    {
        private int editId = 0;
        private UserType editModel = null;
        public UserTypeAddEditFrm(int editId)
        {
            InitializeComponent();
            this.editId = editId;
            Load += UserTypeAddEditFrm_Load;
            btnSave.Click += btnSave_Click;
        }

        private void UserTypeAddEditFrm_Load(object sender, EventArgs e)
        {
            if (editId == 0)
            {
                Text = "添加用户类型";
                txtUserTypeName.Text = "";
                rtbRemark.Text = "";
                btnSave.Text = "添加";
            }
            else
            {
                Text = "修改用户类型";
                editModel = SqlSugarHelper.Db.Queryable<UserType>().Single(ut => ut.UserTypeId == editId);
                if (editModel != null)
                {
                    txtUserTypeName.Text = editModel.UserTypeName;
                    rtbRemark.Text = editModel.Remark;
                }
                btnSave.Text = "编辑";
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(txtUserTypeName.Text))
            {
                MessageBox.Show("用户类型名称不能为空！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (editId == 0)
            {
                var model = new UserType()
                {
                    UserTypeName = txtUserTypeName.Text,
                    Remark = rtbRemark.Text
                };
                int count = SqlSugarHelper.Db.Queryable<UserType>().Where(ut => ut.UserTypeName == model.UserTypeName && ut.Status == 0).Count();
                if (count > 0)
                {
                    MessageBox.Show("用户类型名称重复！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                int row = SqlSugarHelper.Db.Insertable(model).ExecuteCommand();
                if (row == 0)
                {
                    MessageBox.Show("添加数据有误！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
            else
            {
                if (editModel == null)
                {
                    MessageBox.Show("修改数据有误，可能非法进入！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                editModel.UserTypeName = txtUserTypeName.Text;
                editModel.Remark = rtbRemark.Text;
                editModel.LastUpdateUserId = LoginHelper.User.UserId;
                editModel.LastUpdateTime = DateTime.Now;

                int count = SqlSugarHelper.Db.Queryable<UserType>().Where(ut => ut.UserTypeName == editModel.UserTypeName && ut.UserTypeId != editModel.UserTypeId && ut.Status == 0).Count();
                if (count > 0)
                {
                    MessageBox.Show("用户类型名称重复！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                int row = SqlSugarHelper.Db.Updateable(editModel).ExecuteCommand();
                if (row == 0)
                {
                    MessageBox.Show("修改数据有误！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
            DialogResult = DialogResult.OK;
        }
    }
}
